﻿using Autofac;
using TbShop.Business.Components;
using TbShop.Entity;
using HeinQi.Framework.Common.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;

namespace TbShop.Web.Helpers
{
    public class CodeTableHelper
    {
        public static IEnumerable<CodeTable> GetCodeTableDataByCategory(string category)
        {
            var codeTableData = CacheFactory.Get<IEnumerable<CodeTable>>(category);
            if (codeTableData == null || codeTableData.Count() == 0)
            {
                ServiceComponents<CodeTable> codeTableBC = new ServiceComponents<CodeTable>();
                codeTableData = codeTableBC.GetAll().Where(p => p.CodeCategory == category).OrderBy(x => x.OrderNum).ToArray();
                CacheFactory.Set(category, codeTableData);
            }
            return codeTableData;
        }

        public static string GetCodeDescriptionWithCategoryAndCode(string category, string code)
        {
            var codeDescription = string.Empty;
            if (!string.IsNullOrEmpty(code))
            {
                var codeTableData = GetCodeTableDataByCategory(category);
                if (codeTableData != null && codeTableData.Count() > 0)
                {
                    var codeTable = codeTableData.FirstOrDefault(c => c.Code == code);
                    codeDescription = codeTable != null ? codeTable.CodeDescription : string.Empty;
                }
            }
            return string.IsNullOrEmpty(codeDescription) ? string.Empty : codeDescription;
        }
    }
}